package dynamic.metadata;

import dynamic.metadata.property.DynamicTableName;
import dynamic.metadata.property.KeySequence;
import lombok.Setter;

import java.util.Map;

public class EntityClassImpl implements EntityClass {

    private final EntityClass superClass;


    private final String packageName;

    private final KeySequence keySequence;

    private final String simpleName;

    private  Map<String, EntityProperty> properties;
    private boolean existTableId =  false;

    @Setter
    private  DynamicTableName  dynamicTableName =  null;
    public void setProperties(Map<String, EntityProperty> properties) {
        this.properties = properties;
        for(EntityProperty entityProperty:properties.values()){
            if (entityProperty.getTableId() != null) {
                this.existTableId = true;
                break;
            }
        }
    }



    private final TableName tableName;


    public EntityClassImpl (EntityClass superClass, String packageName,  KeySequence keySequence, String simpleName,  boolean existTableId, TableName tableName){

        this.superClass = superClass;
        this.packageName = packageName;
        this.keySequence = keySequence;
        this.simpleName = simpleName;
        this.existTableId = existTableId;
        this.tableName = tableName;
    }

    public EntityClassImpl(String packageName,  KeySequence keySequence, String simpleName,  boolean existTableId, TableName tableName){
        this.packageName = packageName;
        this.tableName = tableName;
        this.superClass = null;
        this.keySequence = keySequence;
        this.simpleName = simpleName;
        this.existTableId = existTableId;

    }
    public EntityClassImpl(String packageName,  KeySequence keySequence, String simpleName,  TableName tableName){
        this.packageName = packageName;
        this.tableName = tableName;
        this.superClass = null;
        this.keySequence = keySequence;
        this.simpleName = simpleName;
    }



    public EntityClassImpl( String simpleName,  TableName tableName, String packageName){
        this(packageName,null,simpleName, tableName);
    }

    public EntityClassImpl(String packageName,  String simpleName,  String  tableName, String schema){
        this.packageName = packageName;


        this.tableName = new TableName(tableName,schema);
        this.superClass = null;

        this.keySequence = null;
        this.simpleName = simpleName;
    }

    public EntityClassImpl(String packageName,  String simpleName){
        this.packageName = packageName;
        this.tableName = null;
        this.superClass = null;

        this.keySequence = null;
        this.simpleName = simpleName;

    }


    @Override
    public EntityClass getSuperclass() {
        return superClass;
    }

    @Override
    public String getName() {
        return packageName+"."+simpleName;
    }

    @Override
    public String getSimpleName() {
        return simpleName;
    }

    @Override
    public TableName getTableName() {
        return tableName;
    }

    @Override
    public Map<String, EntityProperty> getProperties() {
        return properties;
    }

    @Override
    public boolean isExistTableId() {
        return existTableId;
    }

    @Override
    public KeySequence getKeySequence() {
        return keySequence;
    }

    @Override
    public boolean isAssignableFrom(EntityClass tagClass) {
        return this.equals(tagClass);
    }

    @Override
    public DynamicTableName getDynamicTableName() {
        return dynamicTableName;
    }

    @Override
    public EntityProperty getProperty(String propertyName) {
        return properties.get(propertyName);
    }

}
